package com.api.controller;

import com.api.config.SseEmitterServer;
import com.api.entity.Mailbox;
import com.api.log.Log;
import com.api.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * @Author: yangsf
 * @CreateDate: 2024/10/21
 */

@Slf4j
@RestController
@CrossOrigin(origins = {"http://localhost","http://vip.yangsf.cn","http://43.143.143.172","http://122.51.222.243"}, maxAge = 3600)
@RequestMapping("/sse")
public class SSEController extends BaseController {

    @Autowired
    SseEmitterServer sseServer;


    @Log("用户连接信箱系统")
    @RequestMapping("/connect")
    public SseEmitter connect(){
        SseEmitter sse = sseServer.connect(getUserId() + "");
        return sse;
    }

    @Log("推送消息")
    @RequestMapping("/send")
    public Result send(){
        Mailbox mb = Mailbox.create().setKind(1).setTitle("标题标题标题标题");
        sseServer.sendMsg(1L, mb);
        return Result.ok();
    }



}
